class Solution:
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        pre = 0
        cnt = defaultdict(int)
        ans = 0
        cnt[0]+=1 # 忘记初始化
        for x in nums:
            pre += x
            if pre >= goal:
                ans += cnt[pre - goal]
            cnt[pre] += 1
        return ans